<template>
  <div class="tools-li">
    <transition name="el-zoom-in-top">
      <div
        v-show="isShow"
        :key="tooltipId"
        ref="customAlert"
        class="custom-demo"
        :style="customDemo"
        transiton="el-zoom-in-center"
      >
        <div class="custom-body">
          <div class="custom-header">
            <div class="user-image">
              <el-image
                class="user-tx"
                :src="
                  $store.getters.user.avatarName === null
                    ? tx
                    : baseURL.substring(0, baseURL.length - 1) +
                      $store.getters.user.avatarName
                "
              />
            </div>
            <div class="user-info">
              <div class="user-info-li">{{ $store.getters.user.nickName }}</div>
              <div class="user-info-li">{{ $store.getters.user.username }}</div>
            </div>
          </div>
          <div class="custom-button" :style="customButton">
            <el-button
              type="text"
              :style="customDemo"
              icon="el-icon-switch-button"
              @click="Logout"
            >退出登录</el-button>
            <el-button
              v-if="$store.getters.user.username === 'admin'"
              :style="customDemo"
              type="text"
              icon="el-icon-monitor"
              @click="toConsole"
            >控制台</el-button>
          </div>
        </div>
      </div>
    </transition>
    <slot name="user" />
  </div>
</template>

<script>
import { generateId } from 'element-ui/src/utils/util';
import { mapGetters } from 'vuex';
import setImage from '@/assets/images/user.png';
import { queryAllSystem } from '@/api/system/usystem';
import Config from '@/settings';
export default {
  name: 'PortalUserInformation',
  props: {
    trigger: {
      type: String,
      default: 'click',
      validator: value =>
        ['click', 'focus', 'hover', 'manual'].indexOf(value) > -1
    }
  },
  data() {
    return {
      tx: setImage,
      baseURL: process.env.VUE_APP_BASE_API, // api 的 base_url
      isShow: false,
      isLogin: false,
      systemList: [],
      customDemo: {},
      customButton: {}
    };
  },
  computed: {
    themes() {
      return this.$store.getters.theme;
    },
    ...mapGetters(['user']),
    tooltipId() {
      return `el-custom-user-${generateId()}`;
    }
  },

  watch: {
    themes(val, oldVal) {
      this.customDemo.background = val.backGroundColor;
      this.customDemo.color = val.fontColor;
      this.customButton.background = val.backGroundColor;
    }
  },
  // created() {
  //   this.themes = this.$store.getters.theme;
  // },
  mounted() {
    if (this.trigger === 'click' && undefined !== this.$refs.customAlert) {
      // click 事件
      this.$refs.customAlert.addEventListener('click', this.triggerOnClick);
    }
    if (this.trigger === 'hover' && undefined !== this.$refs.customAlert) {
      this.$refs.customAlert.addEventListener('mouseenter', this.triggerOpen);
      this.$refs.customAlert.addEventListener('mouseleave', this.triggerClose);
    }
  },
  destroyed() {
    if (this.trigger === 'click' && undefined !== this.$refs.customAlert) {
      // click 事件
      this.$refs.customAlert.removeEventListener('click', this.triggerOnClick);
    }
    if (this.trigger === 'hover' && undefined !== this.$refs.customAlert) {
      this.$refs.customAlert.removeEventListener(
        'mouseenter',
        this.triggerOpen
      );
      this.$refs.customAlert.removeEventListener(
        'mouseleave',
        this.triggerClose
      );
    }
  },
  methods: {
    triggerOnClick() {
      if (this.isShow) {
        this.isShow = false;
      } else {
        this.isShow = true;
      }
    },
    triggerOpen() {
      this.isShow = true;
      this.$emit('callBack', this.isShow);
    },
    triggerClose() {
      this.isShow = false;
      this.$emit('callBack', this.isShow);
    },
    Logout() {
      this.$confirm('确定注销并退出系统吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.userLoginOut();
        })
        .catch(() => {
          this.$message.success('取消成功！');
        });
    },
    toConsole() {
      window.open('/dashboard', '_blank');
    },
    userLoginOut() {
      const loading = this.$loading({
        lock: true,
        text: 'Loading'
      });
      queryAllSystem()
        .then(res => {
          const { content } = res;
          this.systemList = content;
          this.$store
            .dispatch('LogOut')
            .then(() => {
              if (Config.isCas) {
                setTimeout(() => {
                  loading.close();
                }, 600);
                window.location.href = Config.casLogout;
              } else {
                setTimeout(() => {
                  loading.close();
                }, 600);
                location.reload();
              }
            })
            .catch(() => {
              if (Config.isCas) {
                setTimeout(() => {
                  loading.close();
                }, 600);
                window.location.href = Config.casLogout;
              } else {
                setTimeout(() => {
                  loading.close();
                }, 600);
                location.reload();
              }
            });
        })
        .catch(() => {
          this.$store
            .dispatch('LogOut')
            .then(() => {
              if (Config.isCas) {
                loading.close();
                window.location.href = Config.casLogout;
              } else {
                loading.close();
                location.reload();
              }
            })
            .catch(() => {
              if (Config.isCas) {
                loading.close();
                window.location.href = Config.casLogout;
              } else {
                loading.close();
                location.reload();
              }
            });
        });
    }
  }
};
</script>

<style scoped>
.tools-li {
  width: 100%;
  height: 100%;
  position: relative;
}
.custom-demo {
  position: absolute;
  top: 60px;
  right: 0px;
  height: 140px;
  width: 200px;
  z-index: 100;
}
.custom-body {
  width: 100%;
  height: 100%;
  position: relative;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding: 10px;
  box-sizing: border-box;
  display: flex;
  flex-flow: column nowrap;
}
.custom-header {
  flex: 0 1 auto;
  height: 80px;
  display: flex;
  flex-flow: row nowrap;
}
.custom-button {
  flex: 0 1 auto;
  height: 40px;
  text-align: left;
}
.user-image {
  flex: 0 1 auto;
  width: 70px;
  height: 80px;
  padding: 10px 0px;
  box-sizing: border-box;
}
.user-info {
  height: 80px;
  flex: 1 1 auto;
  padding: 10px 0px;
  box-sizing: border-box;
}
.user-info-li {
  height: 30px;
  line-height: 30px;
  text-align: left;
  width: 100%;
}
.user-tx {
  height: 60px;
  margin: 0 auto;
}
</style>
